package com.ssy.bus.controller;

import com.ssy.bus.pojo.Outpatient;
import com.ssy.bus.service.IOutpatientService;
import com.ssy.bus.vo.OutpatientVo;
import com.ssy.common.Constant;
import com.ssy.common.utils.MD5Utils;
import com.ssy.common.utils.PageResult;
import com.ssy.common.utils.Result;
import com.ssy.system.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author ssy
 */
@RestController
@RequestMapping("/outpatient")
public class OutpatientController {

    @Autowired
    private IOutpatientService outpatientService;

    /**
     * 挂号列表的分页
     *
     * @param outpatientVo outpatientVo
     * @return data
     */
    @GetMapping("/loadOutpatientPage")
    public PageResult loadOutpatientPage(OutpatientVo outpatientVo) {
        return this.outpatientService.selectOutpatientByPage(outpatientVo);
    }

    /**
     * 添加Outpatient
     *
     * @param outpatient Outpatient
     * @return data
     */
    @PostMapping("/addOutpatient")
    public Result addOutpatient(Outpatient outpatient) {
        //使用时间戳作为ID
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        String id = sdf.format(date).substring(2);
        outpatient.setId(id);
        //使用随机数作为流水号
        outpatient.setNumber(MD5Utils.getRandom(5));
        outpatient.setState(1);
        outpatient.setSeeTime(new Date());
        return this.outpatientService.insertOutpatient(outpatient);
    }

    /**
     * 加载挂号弹出层的数据表格
     *
     * @return data
     */
    @GetMapping("/loadSeeDoctorPage")
    public Result loadSeeDoctorPage(HttpSession session) {
        User user = (User) session.getAttribute(Constant.SESSION_CURRENT_USER);
        return this.outpatientService.selectSeeDoctorPageByDepId(user.getDepId());
    }

    /**
     * 根据流水号查询患者信息
     *
     * @param number 流水号
     * @return data
     */
    @GetMapping("/selectOutpatientByNumber/{number}")
    public Result selectOutpatientByNumber(@PathVariable String number) {
        return this.outpatientService.selectOutpatientByNumber(number);
    }


}
